<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="流程类型" prop="bpmnType">
        <el-select v-model="queryParams.bpmnType" placeholder="请选择流程类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_audit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部署时间">
        <el-date-picker
          v-model="daterangeDeployTime"
          style="width: 380px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-primary"
          size="mini"
          @click="bpmnDesigner = true"
          v-hasPermi="['workflow:bpmnInfo:deploy']"
        >设计流程图
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-primary"
          size="mini"
          @click="handleDeploy"
          v-hasPermi="['workflow:bpmnInfo:deploy']"
        >流程文件部署
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bpmninfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="流程名称" align="center" prop="bpmnLabel"/>
      <el-table-column label="流程定义 key" align="center" prop="processDefinitionKey"/>
      <el-table-column label="部署时间" align="center" prop="deployTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deployTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="版本号" align="center" prop="version"/>
      <el-table-column label="描述信息" align="center" prop="info"/>

      <el-table-column label="流程文件" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['workflow:bpmnInfo:readResource']"
            @click="bpmnView(scope.row.id)"
          >查看
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="流程图" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['workflow:bpmnInfo:readResource']"
            @click="imageView(scope.row.id)"
          >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleNodeList(scope.row.id)"
            v-hasPermi="['workflow:bpmnInfo:remove']"
          >流程节点列表
          </el-button>

          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workflow:bpmnInfo:remove']"
          >撤销
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 添加或修改流程定义信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="流程类型" prop="bpmnType">
          <el-select v-model="form.bpmnType" placeholder="请选择流程的类型">
            <el-option
              v-for="dict in dict.type.sys_audit_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!--        :auto-upload="false"-->
        <el-form-item label="流程文件" prop="bpmnFile">
          <el-upload action="/" :limit="1"
                     :auto-upload="false"
                     :on-change="fileChange"
                     :file-list="fileList"> <!--保存当前选中的文件-->
            <el-button size="small" type="success">选择文件</el-button>
            <div slot="tip" class="el-upload__tip"><b style="color: red">只能上传.bpmn或.bpmn20.xml文件，且不超过500kb</b>
            </div>
          </el-upload>
        </el-form-item>

        <el-form-item label="备注" prop="info">
          <el-input type="textarea" v-model="form.info" placeholder="请输入备注信息"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="流程图设计" :visible.sync="bpmnDesigner" width="1200px" append-to-body>
      <vue-bpmn style="overflow: hidden; height: 570px;" product="activiti"></vue-bpmn>
    </el-dialog>

    <!-- TODO    流程图显示-->
    <el-dialog title="流程图" :visible.sync="bpmnImageDialog" width="1200px" append-to-body>
      <div v-html="currentBpmnImage"/>
    </el-dialog>

    <!-- TODO    流程部署文件显示-->
    <el-dialog title="流程文件" :visible.sync="bpmnDialog" width="1200px" append-to-body>
      <template>
        <xml-code-mirror :value="currentBpmnFile"/>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {getResource, listBpmninfo, delBpmninfo, deploy} from "@/api/workflow/bpmninfo";
import {getBpmnByInfoId} from "@/api/workflow/bpmnNode"
import VueBpmn from "@/components/bpmn/VueBpmn";
import XmlCodeMirror from '@/components/XmlCodeMiirror'

export default {
  // 代码块插件局部导入
  name: "Bpmninfo",
  // 数据字典
  dicts: ['sys_audit_type'],
  // 局部使用组件
  components: {
    VueBpmn,
    XmlCodeMirror
  },

  data() {
    return {
      bpmnImageDialog: false,/*图弹框*/
      bpmnDialog: false,/*文件弹框*/

      currentBpmnImage: null,//当前流程定义的图片数据
      currentBpmnFile: null,//当前流程定义的文件数据

      bpmnDesigner: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程定义信息表格数据
      bpmninfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 描述信息时间范围
      daterangeDeployTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bpmnType: null,
        deployTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bpmnType: [
          {required: true, message: "请选择流程类型", trigger: "blur"}
        ]
      },
      fileList: [],
    };
  },
  created() {
    this.getList();
    console.log("bpmninfo")
  },
  activated() {

  },
  methods: {
    /** 查询流程定义信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDeployTime && '' != this.daterangeDeployTime) {
        this.queryParams.params["beginDeployTime"] = this.daterangeDeployTime[0];
        this.queryParams.params["endDeployTime"] = this.daterangeDeployTime[1];
      }
      listBpmninfo(this.queryParams).then(response => {
        this.bpmninfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bpmnLabel: null,
        bpmnType: null,
        processDefinitionKey: null,
        deployTime: null,
        version: null,
        info: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDeployTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 部署流程定义 */
    handleDeploy() {
      this.reset();
      this.open = true;
      this.title = "部署流程定义";
    },

    // 将需要的文件赋给 this.fileList
    fileChange(file, fileList) {
      this.fileList = fileList
    },
    /** 提交按钮 */
    submitForm() {
      console.log("部署流程提交")
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.fileList.length <= 0) {
            this.$modal.msgError('请选择要部署的流程文件')
            return
          }
          //表单校验通过 部署流程文件 TODO 参数提交
          let formData = new FormData()
          formData.append('bpmnType', this.form.bpmnType)
          formData.append('file', this.fileList[0].raw)
          formData.append('info', this.form.info)

          //发送请求
          deploy(formData)
            .then(res => {
              // 重置 刷新表格
              this.resetQuery()
              this.open = false
              // 清空fileList中的数据
              this.fileList = []
            })
        }
      })
    },
    /** 删除按钮操作 */
    /*    handleDelete(row) {
          let byInfoId = row.id
           const ids = row.id || this.ids;
           this.$modal.confirm('是否确认删除流程定义信息编号为"' + ids + '"的数据项？').then(function () {
             getBpmnByInfoId(byInfoId).then(res => {
               if (res.data.length > 0) {

                 return
               }
               this.$message.error('错了哦，这是一条错误消息');
               console.log("-----")
               console.log(res)
               console.log(res.data);
               console.log(res.data.length);
             })

             return delBpmninfo(ids);
           }).then(() => {
             this.getList();
             this.$modal.msgSuccess("删除成功");
           }).catch(() => {
           });
        },*/

    handleDelete(row) {
      let byInfoId = row.id
      const ids = row.id || this.ids;

      this.$modal.confirm('是否确认删除流程定义信息编号为"' + ids + '"的数据项？').then(()=> {
        getBpmnByInfoId(byInfoId).then(res => {
          if (res.data.length > 0) {
            this.$message({
              type: 'error',
              message: '存在流程节点，不允许删除!'
            });
            return;
          } else {
            delBpmninfo(ids)
              .then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
              }).catch(() => {

            })
          }
        })
      })

      getBpmnByInfoId(byInfoId).then(res => {
        if (res.data.length > 0) {
          this.$message.error('错了哦，这是一条错误消息');
          console.log("-----")
          console.log(res)
          console.log(res.data);
          console.log(res.data.length);
          return
        }
      })


    },
    // 流程图显示
    imageView(id) {
      let type = 'image'
      //发起请求到后台
      getResource(id, type)
        .then(res => {
          // 将获取到的图片数据赋给当前 并在页面进行显示
          this.currentBpmnImage = res
          this.bpmnImageDialog = true
        })
    }
    ,
    // 文件显示
    bpmnView(id) {
      let type = 'bpmn'
      //发起请求到后台
      getResource(id, type)
        .then(res => {
          console.log(res);
          // 将获取到的文件数据赋给当前 并在页面进行显示
          this.currentBpmnFile = res
          this.bpmnDialog = true
        })

    }
    ,
    /*    /!** 流程节点列表 *!/
        handleNodeList(id) {
          // 进行页面的一个全局路由跳转
          this.$router.push({path: `/flow/bpmnNode/id=${id}`})
        }  */
    /** 流程节点列表 */
    handleNodeList(id) {
      this.$router.push({path: `/flow/bpmnNode?id=${id}`})
    }


  }
};
</script>
